package class1.s2022350143028.data;

import base.data.OrdersBaseData;
import base.model.OrdersBaseModel;
import class1.s2022350143028.model.GoodsModel;
import class1.s2022350143028.model.OrdersModel;
import class1.s2022350143028.utils.DateTimesxyUtil;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 订单信息数据存放，使用集合进行存储数据
 *
 * @author Kcjuly
 * @date 2024-04-25
 *
 */

public class OrdersData extends OrdersBaseData {

    /**自定义集合方式,或选择参考案例模式之一
     * 例如使用ArrayList
     * 继承并实现抽象方法，实现数据增删查改，
     * primaryKey 在重写时的实际使用需要进行类型转换（String/int 等）
     */

    //存储方式自行选择
//    protected List<OrdersBaseModel> ordersDataList;
//    protected Map<String, OrdersBaseModel> ordersDataMap;
    public List<OrdersModel> OrdersDataList;
    DateTimesxyUtil timeutil =new DateTimesxyUtil();


    //todo 重写构造方法，实现new对像时直接初始化数据
    public OrdersData(){
        OrdersDataList =new ArrayList<OrdersModel> ();
        OrdersModel ordersModel=new OrdersModel();

        ordersModel.setOrderId("S1001");

        LocalDateTime datatime=LocalDateTime.now();
        ordersModel.setOrderTime(datatime);

        ordersModel.setGoodsId("G1001");
        ordersModel.setClientName("张三");
        ordersModel.setQuantity(5);
        ordersModel.setPrice(55);
        ordersModel.setGoodsName("曲奇饼干");

        OrdersModel ordersModel1=new OrdersModel();

        ordersModel1.setOrderId("S1002");

        LocalDateTime datatime1=LocalDateTime.now();
        ordersModel1.setOrderTime(datatime1);

        ordersModel1.setGoodsId("G1001");
        ordersModel1.setClientName("李四");
        ordersModel1.setQuantity(3);
        ordersModel1.setPrice(55);
        ordersModel1.setGoodsName("曲奇饼干");
        
        OrdersDataList.add(ordersModel);
        OrdersDataList.add(ordersModel1);

    }


    /**
     * 查询订单
     * @param query 查询条件, choice 控制哪种查询
     * @return boolean
     */
    public boolean Get(Object query,int choice){
        String.valueOf(query);
        //订单编号查询
        boolean flag=false;
        if(choice==1){
            for (int i = 0; i < OrdersDataList.size(); i++){
                OrdersModel ordersModel = OrdersDataList.get(i);
                if(ordersModel.getOrderId().equals(query)){
                    LocalDateTime datatime=ordersModel.getOrderTime();
                    String formatter=timeutil.DateFormat(datatime,"yyyy-MM-dd");
                    System.out.format("%15s %10s %8s %10s %20s %4d %4d %8f\n",formatter,ordersModel.getOrderId(),ordersModel.getClientName(),ordersModel.getgoodsName(),ordersModel.getgoodsId(),ordersModel.getprice(),ordersModel.getQuantity(),ordersModel.getTotalPrice());
                    flag=true;
                }
            }
        }
        //客户名称查询
        else if(choice==2){
            for (int i = 0; i < OrdersDataList.size(); i++){
                OrdersModel ordersModel = OrdersDataList.get(i);
                if(ordersModel.getClientName().equals(query)){
                    LocalDateTime datatime=ordersModel.getOrderTime();
                    String formatter=timeutil.DateFormat(datatime,"yyyy-MM-dd");
                    System.out.format("%15s %10s %8s %10s %20s %4d %4d %8f\n",formatter,ordersModel.getOrderId(),ordersModel.getClientName(),ordersModel.getgoodsName(),ordersModel.getgoodsId(),ordersModel.getprice(),ordersModel.getQuantity(),ordersModel.getTotalPrice());
                    flag=true;
                }
            }
        }
        //日期段查询
        else if(choice==3){

            for (int i = 0; i < OrdersDataList.size(); i++){
                OrdersModel ordersModel = OrdersDataList.get(i);
                LocalDateTime datatime=ordersModel.getOrderTime();
                String formatter=timeutil.DateFormat(datatime,"yyyy-MM-dd");
                if(formatter.equals(query)){
                    System.out.format("%15s %10s %8s %10s %20s %4d %4d %8f\n",formatter,ordersModel.getOrderId(),ordersModel.getClientName(),ordersModel.getgoodsName(),ordersModel.getgoodsId(),ordersModel.getprice(),ordersModel.getQuantity(),ordersModel.getTotalPrice());
                    flag=true;
                }
            }
        }
        if(flag)return true;
        else{
            System.out.println("查无此订单");
            return false;
        }

    }


    /**
     * 录入订单
     * @param ordersBaseModel 要保存的数据模型
     * @return boolean
     */
    public boolean Save(OrdersModel ordersBaseModel, List<GoodsModel> goodlist){
        ordersDataList.add(ordersBaseModel);
        //更改该商品的库存
        for (OrdersModel orders:OrdersDataList){
            if(orders.getOrderId().equals(ordersBaseModel.getOrderId())){
                String x=orders.getgoodsId();
                for(GoodsModel good:goodlist){
                    if(good.getGoodId().equals(x)){
                        int y=good.getInventory();
                        y-=orders.getQuantity();
                        good.setinventory(y);
                        break;
                    }
                }
                break;
            }
        }
//        ordersDataList.add(new Order_Model(today,ordersBaseModel.getorderId(),ordersBaseModel.getclientName(),ordersBaseModel.getgoodsName(),ordersBaseModel.getgoodsId(),ordersBaseModel.getprice(),ordersBaseModel.getquantity()));
        return true;
    }


    /**
     * 更新订单
     * @param ordersBaseModel 要保存的数据模型
     * @return boolean
     */
    public boolean Update(OrdersModel ordersBaseModel, List<GoodsModel> goodlist){
        int data=ordersBaseModel.getQuantity();
        for (OrdersModel orders:OrdersDataList){
            if(orders.getOrderId().equals(ordersBaseModel.getOrderId())){
                orders.setClientName(ordersBaseModel.getClientName());
                orders.setGoodsName(ordersBaseModel.getgoodsName());
                orders.setGoodsId(ordersBaseModel.getgoodsId());
                orders.setPrice(ordersBaseModel.getprice());
                //当编辑订单数量时修改商品库存
                if(orders.getQuantity()!=data){
                    int last=data-orders.getQuantity();
                    String x=orders.getgoodsId();
                    for(GoodsModel good:goodlist){
                        if(good.getGoodId().equals(x)){
                            int y=good.getInventory();
                            y-=last;
                            good.setinventory(y);
                            break;
                        }
                    }

                }
                orders.setQuantity(data);
                return true;
            }
        }
        return false;
    }


    /**
     * 删除订单
     * @param primaryKey 根据订单编号删除
     * @return boolean
     */
    public boolean Delete(String primaryKey, List<GoodsModel> goodlist){
        Iterator<OrdersModel> iterator = OrdersDataList.iterator();
        while (iterator.hasNext()) {
            OrdersModel ordersModel = iterator.next();
            if (ordersModel.getOrderId().equals(primaryKey)) {
                String goodId=ordersModel.getgoodsId();
                int q=ordersModel.getQuantity();
                for(GoodsModel good:goodlist){
                    if(good.getGoodId().equals(goodId)){
                        int y=good.getInventory();
                        y+=q;
                        good.setinventory(y);
                        break;
                    }
                }
                iterator.remove(); // 使用Iterator的remove方法删除元素
                return true;
            }

        }
        return false;
    }

    @Override
    public List<OrdersBaseModel> get(Object query) {
        return null;
    }

    @Override
    public boolean save(OrdersBaseModel ordersBaseModel) {
        return false;
    }

    @Override
    public boolean update(OrdersBaseModel ordersBaseModel) {
        return false;
    }

    @Override
    public boolean delete(Object primaryKey) {
        return false;
    }

}
